home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dump1a.zip / DUMP.C next >
C/C++ Source or Header  |  1990-09-13  |  4KB  |  117 lines

  1. /* ---------- DUMP.C 
  2.    ---------- Placed in the public domain on 25 August 1990 by
  3.    ----------            Richard M. Utter, ProLogic
  4.    ---------- This is an alternative to the file dump capability provided by
  5.    ---------- DOS's DEBUG. It was hastily batted out after I grew tired of
  6.    ---------- DEBUG's inability to provide proper values for file offsets and
  7.    ---------- to display anything other than bytes. You are warned: DUMP is far
  8.    ---------- from perfect. It's fairly crummy code. But it gets the job as I
  9.    ---------- defined it (dump longwords and ASCII with correct file offsets)
  10.    ---------- done.
  11.    ---------- Command line: DUMP file_name
  12.    ---------- After the initial data appears, press any key other than <ESC> to
  13.    ---------- see more. Press <ESC> any time to exit. Note that DUMP substitutes
  14.    ---------- periods for control characters from 0 to 0x1f in the righthand
  15.    ---------- ASCII dump.
  16. */
  17. #include <conio.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. /* ---------- Define display lines and the buffer size in terms of both
  23.    ---------- characters and longwords.
  24. */
  25. #define nlines 16
  26. #define clength nlines * 16
  27. #define llength nlines * 4
  28. /* ---------- Executable statements */
  29. main (int argc, char * argv[])
  30. {
  31.     FILE * infile = NULL;
  32.     unsigned char * ucp, * xcp;
  33.     unsigned char string[17];
  34.     int lines, i, j;
  35.     unsigned long int buffer[llength], offset = 0;
  36.     size_t chars_read;
  37.     string[16] = '\0';
  38. /* ---------- Drop dead if file name is missing from command line. */
  39.     if (argc < 2)
  40.     {
  41.         printf ("\nUse \"DUMP file_name\"\n");
  42.         return 1;
  43.     }
  44.     infile = fopen (argv[1], "rb");
  45. /* ---------- Bail out if file name won't open. */
  46.     if (infile == NULL)
  47.     {
  48.         printf ("File open error...");
  49.         return 2;
  50.     }
  51. /* ---------- Finally: dump the data. */
  52.     while ((!feof (infile)) && (!ferror (infile)))
  53.     {
  54. /* ---------- Get "clength" characters. */
  55.         chars_read = fread (buffer, (size_t) 1, (size_t) (clength), infile);
  56. /* ---------- Point at start of buffer. */
  57.         ucp = (unsigned char *) buffer;
  58. /* ---------- How many full lines are there? */
  59.         lines = chars_read / 16;
  60.         printf ("\n F E D C  B A 9 8  7 6 5 4  3 2 1 0\n");
  61.         for (i = 0; i < lines; i++)
  62.         {
  63. /* ---------- Hex dump. */
  64.             for (j = 3; j >= 0; j--)
  65.                 printf ("%8.8lx ", buffer[i * 4 + j]);
  66. /* ---------- ASCII dump. */
  67.             memcpy (string, ucp, (size_t) 16);
  68.             for (j = 0; j < 16; j++, ucp++)
  69.                 if (string[j] < 32)
  70.                     string[j] = '.';
  71.             printf ("    %s   %8.8lx  %ld\n", string, offset, offset);
  72.             offset += 16;
  73.         }
  74. /* ---------- Deal with possible final truncated line. */
  75.         i = chars_read % 16;
  76.         if (i)
  77.         {
  78. /* ---------- Point to correct location in buffer. */
  79.             xcp = ucp;
  80.             for (j = 1; j < i; j++, xcp++)
  81.                 ;
  82. /* ---------- Print blanks for any missing bytes. */
  83.             for (j = 1; j <= 16 - i; j++)
  84.             {
  85.                 if ((j == 4) || (j == 8) || (j == 12))
  86.                     printf (" ");
  87.                 printf ("  ");
  88.             }
  89. /* ---------- Hex dump. */
  90.             for (j = i; j > 0; j--, xcp--)
  91.             {
  92.                 if ((j != i) && (j % 4 == 0))
  93.                     printf (" ");
  94.                 printf ("%2.2x", *xcp);
  95.             }
  96.             printf (" ");
  97. /* ---------- ASCII dump. */
  98.             memcpy (string, ucp, (size_t) i);
  99.             for (j = 0; j < i; j++)
  100.                 if (string[j] < 32)
  101.                     string[j] = '.';
  102.             for (j =i; j < 16; j++)
  103.                 string[j] = ' ';
  104.             printf ("    %s   %8.8lx  %ld\n", string, offset, offset);
  105.         }
  106.         printf ("\n");
  107. /* ---------- Get operator input. Any character other than <ESC> displays
  108.    ---------- next "clength" bytes.
  109. */
  110.         i = getch ();
  111.         if (i == 27)
  112.             break;
  113.     }
  114.     fclose (infile);
  115.     return EXIT_SUCCESS;
  116. }
  117.